package org.cxa.rpc.cloud.configurationConsole;

import java.awt.CardLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.cxa.rpc.cloud.client.TransientDevice;
import org.cxa.rpc.cloud.configurationConsole.LoginPanel.LoginListener;
import org.cxa.rpc.transportChannel.TransportChannel.DisconnectReason;

import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.border.BevelBorder;


public class ConfigurationConsole extends JFrame
{
	private static final long serialVersionUID = -1887265539800501031L;

	private final TransientDevice tDev;
	
	private JPanel pnl_center;
	private MainPanel pnl_main;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable()
		{
			public void run() {
				try
				{
					ConfigurationConsole frame = new ConfigurationConsole();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConfigurationConsole()
	{
		// setup our connection to the server
		this.tDev = new TransientDevice(false);
		
		setTitle("RPC Server Configuration Console -- opencxa.org");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(null);
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		pnl_center = new JPanel();
		contentPane.add(pnl_center, BorderLayout.CENTER);
		pnl_center.setLayout(new CardLayout(0, 0));
		
		JPanel pnl_loginContainer = new JPanel();
		pnl_center.add(pnl_loginContainer, getIdForPanel(pnl_loginContainer));
		pnl_loginContainer.setLayout(new BoxLayout(pnl_loginContainer, BoxLayout.X_AXIS));
		
		Component horizontalGlue = Box.createHorizontalGlue();
		pnl_loginContainer.add(horizontalGlue);
		LoginPanel pnl_login = new LoginPanel(this.tDev);
		pnl_login.setAlignmentX(Component.CENTER_ALIGNMENT);
		pnl_loginContainer.add(pnl_login);
		
		Component horizontalGlue_1 = Box.createHorizontalGlue();
		pnl_loginContainer.add(horizontalGlue_1);
		pnl_login.addLoginListener(new LoginListener()
		{
			@Override
			public void onLoginSuccessful()
			{
				((CardLayout)pnl_center.getLayout()).show(pnl_center, getIdForPanel(pnl_main));
			}
			
			@Override
			public void onLogout(DisconnectReason reasonIn, boolean willAttemptReconnectIn)
			{
				((CardLayout)pnl_center.getLayout()).show(pnl_center, getIdForPanel(pnl_loginContainer));
			}
		});
		
		this.pnl_main = new MainPanel(this.tDev);
		pnl_center.add(pnl_main, getIdForPanel(pnl_main));
		
		FooterPanel footerPanel = new FooterPanel(this.tDev);
		footerPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		contentPane.add(footerPanel, BorderLayout.SOUTH);
	}
	
	
	private static String getIdForPanel(JPanel panelIn)
	{
		return Integer.valueOf(System.identityHashCode(panelIn)).toString();
	}
}
